package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.adapter.CountrySpinnerAdapter;
import cm.mefobe.android.telecomcost.entities.Country;
import cm.mefobe.android.telecomcost.entities.Zone;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

public class CountrySpinner extends Spinner {

	private ArrayList<Country> _countries;
	private Context _context;
	private SpinnerAdapter _adapter;
	private ZoneSpinner _zoneSpinner;
	private Country _country;
	private Zone _zone;	

	@Override
	public void setSelection(int position) {
		Log.i("CountrySpinner", "setting selection..." + position);
		_country = _countries.get(position);
		super.setSelection(position);
	}

	public ArrayList<Country> getCountries() {		
		if(_countries == null)
		{
			_countries = new ArrayList<Country>();
		}
		return _countries;
	}

	public Country getSelectedCountry() {
		if (_country == null) {
			if (_adapter.getCount() > 0) {
				_country = (Country) _adapter.getItem(0);
			}
		}

		return _country.getId() != -1 ? _country : null;
	}

	private void setList() {
		if (_context != null) {
			if (_zoneSpinner != null) {
				if (_zoneSpinner.getSelectedZone() != null) {
					Log.i("CountrySpinner", "binding Countries...");
					_countries = _zoneSpinner.getSelectedZone().getCountries();					
				} else {
					if (_zoneSpinner.getFirstZone() == null) {
						Log.w("CountrySpinner", "no zone selected...");
						_countries = new ArrayList<Country>();
					} else {
						Log.i("CountrySpinner",
								"we select the first zone...zoneSpinner.getFirstZone()...");
						_countries = _zoneSpinner.getFirstZone().getCountries();
					}
				}

				_countries.add(0, Country.allCountry());
				_adapter = new CountrySpinnerAdapter(_context,
						android.R.layout.select_dialog_singlechoice, _countries);
			} else {
				Log.w("CountrySpinner",
						"cannot set the adapter of the CountrySpinner...zoneSpinner is null...");
			}
		} else {
			Log.w("CountrySpinner",
					"cannot set the adapter of the CountrySpinner...the context is null");
		}
	}

	@Override
	public void setAdapter(SpinnerAdapter adapter) {
		Log.i("CountrySpinner", "setting the adapter...");
		setList();
		_country = _adapter.getCount() > 0 ? _countries.get(0) : null;
		super.setAdapter(_adapter);
	}

	@Override
	public SpinnerAdapter getAdapter() {
		if (_adapter == null) {
			setList();
		}
		if (_adapter == null) {
			Log.i("CountrySpinner", "could not set the adapter...");
		}
		return _adapter;
	}

	public CountrySpinner(Context context, ZoneSpinner zoneSpinner) {
		super(context);
		_context = context;

		setZoneSpinner(zoneSpinner);
	}

	public ZoneSpinner getZoneSpinner() {
		return _zoneSpinner;
	}

	public void setZoneSpinner(ZoneSpinner zoneSpinner) {
		if (_context != null) {
			if (zoneSpinner != null) {
				this._zoneSpinner = zoneSpinner;
				_zoneSpinner
						.setOnItemSelectedListener(new OnItemSelectedListener() {

							public void onItemSelected(AdapterView<?> arg0,
									View arg1, int arg2, long arg3) {
								Log.i("_zoneSpinner", "onItemSelected");
								setAdapter(null);
							}

							public void onNothingSelected(AdapterView<?> arg0) {
								Log.i("CountrySpinner", "onNothingSelected");
								setAdapter(null);
							}
						});
			} else {
				throw new NullPointerException("the ZoneSpinner cannot be null");
			}
		} else {
			throw new NullPointerException(
					"the _context of the country spinner cannot be null");
		}
	}

	public CountrySpinner(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
	}

	public CountrySpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
	}

	public CountrySpinner(Context context) {
		super(context);
		_context = context;
	}

	public void setSelectedCountry(Country country) {
		this._country = country;
	}

	public void setZone(Zone zone) {
		if (_context != null) {
			this._zone = zone;
			setAdapter(null);
		} else {
			throw new UnsupportedOperationException(
					"cannot setZone...context of the country spinner is null");
		}
	}

	public Zone getZone() {
		return _zone;
	}
}
